<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserLoginRequest;
use App\Http\Requests\UserRegisterRequest;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Image;

class UsersController extends Controller
{
    //
    public function register(){
        return view('users.register');
    }

    public function store(UserRegisterRequest $request){
//        dd($request->all());
        $data = [
            'confirm_code' => str_random(48),
            'avater'=>'/images/default-avater.png'
        ];
        //保存用户数据
        $user = User::create(array_merge($request->all(),$data));
        //发送邮件
        $subject = '验证你的邮箱';
        $view = 'email.register';
        $this->sendTo($user,$subject,$view,$data);
        //重定向
        return redirect('/');
    }

    public function sendTo($user,$subject,$view,$data = []){
//        \Mail::queue();
    }

    public function confirmEmail($confirm_code){
        $user = User::where('confirm_code',$confirm_code)->first();
        if (is_null($user)){
            return redirect('/');
        }
        $user->is_confirmed = 1;
        $user->confirm_code = str_random(48);
        $user->save();
        \Session::flash('邮箱已验证,请登录');
        return redirect('/users/login');
    }

    public function login(){
        return view('users.login');
    }

    public function signin(UserLoginRequest $request){
        if (\Auth::attempt([
            'email' => $request->get('email'),
            'password' => $request->get('password'),
            'is_confirmed' => 1,
        ])){
            return redirect('/');
        }
        \Session::flash('user_login_failed','登录出错');
        return redirect('/users/login')->withInput();
    }

    public function logout(){
        \Auth::logout();
        return redirect('/');
    }

    public function avater(){
        return view('users.avater');
    }

    public function changeAvater(Request $request){
        $file = $request->file('avater');
        $input = array('image'=>$file);
        $rules = array(
            'image' => 'image',
        );
        $validator = Validator::make($input,$rules);
        if ($validator->fails()){
            return \Response::json([
                'success' => false,
                'errors'  => $validator->getMessageBag()->toArray(),
            ]);
        }

        $destinationPath = 'uploads/';
        $filename = \Auth::user()->id . '_' . time() . $file->getClientOriginalName();
        $file->move($destinationPath, $filename);

        Image::make($destinationPath.$filename)->fit(100)->save(); //执行到这后 出错

//        $user = User::find(\Auth::user()->id);
//        $user->avater = '/' . $destinationPath . $filename;
//        $user->save();

        return \Response::json([
            'success' => true,
            'avatar'  => '/' . $destinationPath . $filename,
//            'avatar'  => assert($destinationPath . $filename),
        ]);

//        return redirect('/users/avater');
    }

    public function cropAvater(Request $request){
        $photo = mb_substr($request->get('photo'),1);
        $w = (int) $request->get('w');
        $h = (int) $request->get('h');
        $x = (int) $request->get('x');
        $y = (int) $request->get('y');

        Image::make($photo)->crop($w,$h,$x,$y)->save(); //执行到这后 出错

        $user = \Auth::user();
        $user->avater = $request->get('photo');
        $user->save();

        return redirect('/users/avater');
    }
}
